import lejos.nxt.*;
import lejos.robotics.subsumption.Behavior;

/**
 * Make sure the robot stays within the boundaries of the field.
 */
public class StayInField implements Behavior {
	private boolean suppressed = false;
	private LightSensorValue tape;
	private int turnRate = 150;
	
	/**
	 * Default constructor.
	 */
	public StayInField()
	{
		tape = new LightSensorValue(47, 52);
	}

	/**
	 * Make a turn when we take action.
	 */
	public void action() {
		//right turn
    	Motor.A.rotate(turnRate, true);
		Motor.B.rotate(-turnRate, true);
	}

	/* (non-Javadoc)
	 * @see lejos.robotics.subsumption.Behavior#suppress()
	 */
	public void suppress() {
		suppressed = true;
	}

	/**
	 * Take control if we the light data is within given range.
	 */
	public boolean takeControl() {
		int value = BillManager.GetLightData();
		
		if (value > tape.GetLowerBound() && value < tape.GetUpperBound())
			return true;
		
		return false;
	}

	/**
	 * @author Mark
	 * Internal class to be able to assign areas based on color.
	 */
	private class LightSensorValue {
		private int lowerBound;
		private int upperBound;

		/**
		 * Constructor.
		 * @param lower Lower light data.
		 * @param upper Higher light data.
		 */
		public LightSensorValue(int lower, int upper) {
			this.lowerBound = lower;
			this.upperBound = upper;
		}

		/**
		 * Gets the lower bound.
		 * @return Lower bound.
		 */
		public int GetLowerBound() {
			return lowerBound;
		}

		/**
		 * Gets the higher bound.
		 * @return Higher bound.
		 */
		public int GetUpperBound() {
			return upperBound;
		}
	}
}
